27

Build Your Own Game—Tic Tac Toe

27

STEP 1

Sub ComputerPlay(XScore, OScore, EmptyCount)

Factor(1) = Array(0, 2, 3, 5)

Factor(2) = Array(0, 7, 11, 13)

Factor(3) = Array(0, 17, 19, 23)

ComputerPlayed = False

If Not ComputerPlayed Then

For k = 1 To 8

For i = 1 To 3

For j = 1 To 3

If UserForm1.Controls("TextBox" & (i - 1) * 3 + j) = "" Then

If WorksheetFunction.Gcd(WinTag(k), Factor(i)(j ) * OScore) = WinTag(k) Then

UserForm1.Controls("TextBox" & (i - 1) * 3 + j) = "O"

ComputerPlayed = True

k = 9

i = 4

Exit For

End If

End If

Next

Next

Next

End If

STEP 2

If Not ComputerPlayed Then

For k = 1 To 8

For i = 1 To 3

For j = 1 To 3

If UserForm1.Controls("TextBox" & (i - 1) * 3 + j) = "" Then

If WorksheetFunction.Gcd(WinTag(k), Factor(i)(j) * XScore) = WinTag(k) Then

'If Factor(i)(j) * XScore = WinTag(k) Then

UserForm1.Controls("TextBox" & (i - 1) * 3 + j) = "O"

ComputerPlayed = True

k = 9

i = 4

Exit For

End If

End If

Next

Next

Next

End If

FIGURE 2.25  Computer Play.